package cn.yanglq.Rss.web.service.impl;

import cn.yanglq.Rss.web.dao.UserMapper;
import cn.yanglq.Rss.web.model.User;
import cn.yanglq.Rss.web.model.UserExample;
import cn.yanglq.Rss.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yanglq on 2017/6/12.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    public User selectUserByUserId(String userId) {
        UserExample example = new UserExample();
        example.createCriteria().andUseridEqualTo(userId);
        List<User> user = userMapper.selectByExample(example);
        return user.size() == 1 ? user.get(0) : null;
    }

    public void updatePassword(String userid, String password) {
        User user = userMapper.selectByPrimaryKey(userid);
        user.setPasswd(password);
        userMapper.updateByPrimaryKey(user);

    }

    public User selectUserInfo(String userid) {
        return userMapper.selectByPrimaryKey(userid);
    }

    @Override
    public void updateInfo(String userid, User newuser) {
        User user = userMapper.selectByPrimaryKey(userid);
        user.setMobilenumber(newuser.getMobilenumber());
        user.setUsername(newuser.getUsername());
        user.setEmail(newuser.getEmail());
        userMapper.updateByPrimaryKey(user);
    }

    @Override
    public User selectUserInfoByEmail(String regEmail) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andEmailEqualTo(regEmail);
        List<User> user = userMapper.selectByExample(example);
        return user.size() != 0 ? user.get(0) : null;
    }
}
